﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharepointSolutionInstaller.Views;
using Microsoft.SharePoint.Administration;
using SharepointSolutionInstaller.Resources;
using SharepointSolutionInstaller.Utils.Configuration;

namespace SharepointSolutionInstaller.Utils.SolutionDeployment
{
    public class WaitForJobCompletionCommand : Command
    {
        private readonly string description;
        private DateTime startTime;
        private bool first = true;
        Guid solutionId;
        private static readonly TimeSpan JobTimeout = TimeSpan.FromMinutes(15);

        internal WaitForJobCompletionCommand(InstallProcess parent, string description,Guid solutionId)
            : base(parent)
        {
            this.description = description;
            this.solutionId = solutionId;
        }

        internal override string Description
        {
            get
            {
                return description;
            }
        }

        protected internal override bool Execute()
        {
            try
            {
                SPSolution installedSolution = SPFarm.Local.Solutions[solutionId];

                if (first)
                {
                    if (!installedSolution.JobExists) return true;
                    startTime = DateTime.Now;
                    first = false;
                }

                //
                // Wait for job to end
                //
                if (installedSolution.JobExists)
                {
                    if (DateTime.Now > startTime.Add(JobTimeout))
                    {
                        throw new Exception(CommonUIStrings.installExceptionTimeout);
                    }

                    return false;
                }
                else
                {
                    log.Info(installedSolution.LastOperationDetails);

                    SPSolutionOperationResult result = installedSolution.LastOperationResult;
                    if (result != SPSolutionOperationResult.DeploymentSucceeded && result != SPSolutionOperationResult.RetractionSucceeded)
                    {
                        throw new Exception(installedSolution.LastOperationDetails);
                    }

                    return true;
                }
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        protected internal override bool Rollback()
        {
            SPSolution installedSolution = SPFarm.Local.Solutions[solutionId];

            //
            // Wait for job to end
            //
            if (installedSolution != null)
            {
                if (installedSolution.JobExists)
                {
                    if (DateTime.Now > startTime.Add(JobTimeout))
                    {
                        throw new Exception(CommonUIStrings.installExceptionTimeout);
                    }
                    return false;
                }
                else
                {
                      log.Info(installedSolution.LastOperationDetails);
                }
            }

            return true;
        }
    }
}
